<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace app\portal\controller;

use cmf\controller\BaseController;
use api\mall\model\StoreModel;
use api\mall\model\StoreAttrModel;
use api\mall\model\StoreAttrValueModel;
use api\mall\model\MedalModel;
/**
 * Description of MallController
 *
 * @author PC
 */
class MallController extends BaseController{
    public function index(){
        $id = $this->request->route('id');
        $store = new StoreModel();
        $item = $store->where('id',$id)->find();
        if(empty($item)){
            return $this->error('商品去哪了');
        }
        $attrModel = new StoreAttrModel();
        $attrs = $attrModel->where('item_id',$id)->order('main asc')->select();
        
        $attrValue = new StoreAttrValueModel();
        $values = $attrValue->where('v.item_id',$id)->alias('v')->join('store_attr_map m','v.id=m.attr_value_id','left')
                ->order('order_id asc')->select();
        
        $tmp = [];
        foreach ($attrs as $attr){
            $a = $attr;
            $vlist = [];
            foreach ($values as $value){
                if($value['attr_id']==$attr['id']){
                    $vlist[]=$value;
                }
            }
            $a['values'] = $vlist;
            $tmp[] = $a;
        }
        
        $this->assign($item->toArray());
        $this->assign('attrs',$tmp);
        return $this->fetch();
    }
    
    public function medal()
    {
        $id   = $this->request->route("id", 0, "intval");
        $medal = MedalModel::find($id);

        $avatar = '';
        if (!empty($medal)) {
            $avatar = cmf_get_image_url($medal['medal_url']);
        }
        return redirect($avatar);
    }
}

